home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr44 / wgtrun.zip / RUN1.C < prev    next >
C/C++ Source or Header  |  1995-02-21  |  4KB  |  180 lines

  1. #include <wgt5.h>
  2.  
  3. /* 
  4.  Run1.c
  5.  
  6.  This example shows how to make a scrolling image with depth.  It scrolls
  7.  each horizontal line of the ground by different amounts, the fastest
  8.  being closest to you.  
  9.  
  10. */
  11.  
  12. color pal[256];         /* The palette used for every graphic image */
  13. block sprites[200];     /* Array of images for the running robot */
  14.  
  15. block background;       /* Holds the background scrolling image */
  16. block work;             /* Page for constructing each frame */
  17.  
  18.  
  19. /* A structure which holds the scrolling values for each horizontal line */
  20. typedef struct
  21.  {
  22.   int x;                /* Current x value, shifted by 8 */
  23.   int increment;        /* fixed point increment */
  24.  } line_scroll;
  25. line_scroll lines[80];  /* 80 scrolling lines along the ground */
  26.  
  27. /* A simple sprite structure */
  28. typedef struct
  29.  {
  30.   int x;
  31.   int y;
  32.   int anm;              /* Sprite number */
  33.  } sprite;
  34. sprite people[5];
  35.  
  36.  
  37. int backx = 0;          /* X value for the scrolling rocks */
  38. int backinc;            /* X increment for scrolling rocks */
  39.  
  40.  
  41. /* Loads the graphics files, and allocates buffers */
  42. void load_graphics (void)
  43. {
  44.  work = wallocblock (320, 200);
  45.  /* Allocate a work buffer */
  46.  
  47.  wloadsprites (pal, "run.spr", sprites, 0, 199);
  48.  background = wloadgif ("street.gif", pal);
  49.  wsetpalette (0, 255, pal);
  50. }
  51.  
  52.  
  53.  
  54. /* Frees the buffers and sprites */
  55. void free_graphics (void)
  56. {
  57.  wfreesprites (sprites, 0, 199);
  58.  wfreeblock (background);
  59.  wfreeblock (work);
  60. }
  61.  
  62.  
  63. /* Set up the initial scrolling values */
  64. void init_lines (void)
  65. {
  66. int i;
  67. int inc;
  68.  
  69.  inc = 128;     /* slowest scrolling speed (128/256 of a pixel */
  70.  
  71.  backx = 0;     /* rocks x value */
  72.  backinc = inc; /* rocks same speed as the ground */
  73.  
  74.  for (i = 0; i < 80; i++)
  75.   {
  76.    lines[i].x = 0;              /* clear out the x value */
  77.    lines[i].increment = inc;    /* set the scroll speed */
  78.    inc += 32;                   /* Make the next row move faster */
  79.   }
  80.  
  81. }
  82.  
  83.  
  84.  
  85. /* Construct the background image */
  86. void animate_lines (void)
  87. {
  88. block source1, source2;
  89. block dest1, dest2;
  90. block origsource, origdest;
  91. int i;
  92. int x;
  93.  
  94.  wcopyscreen (0, 0, 319, 51, background, 0, 0, work);
  95.  /* Draw the moon stationary */
  96.  
  97.  
  98.  x = backx >> 8;
  99.  
  100.  /* Copy the rocks */
  101.  wcopyscreen (x, 52, 319, 119, background, 0, 52, work);
  102.  if (x > 0)
  103.    wcopyscreen (0, 52, x-1, 119, background, 320 - x, 52, work);
  104.  
  105.  /* Scroll the rocks */
  106.  backx += backinc;
  107.  if (backx >= 81920)   /* 81920 is 320 << 8 */
  108.      backx -= 81920;
  109.  
  110.  
  111.  origdest = abuf + 120 * 320;           /* First row to copy */
  112.  origsource = background + 120 * 320;   /* First row to copy */
  113.  
  114.  for (i = 0; i < 80; i++)
  115.   {
  116.    /* Scroll this line */
  117.    lines[i].x += lines[i].increment;
  118.    if (lines[i].x >= 81920)   /* 81920 is 320 << 8, wraps scroll around */
  119.       lines[i].x -= 81920;
  120.  
  121.    
  122.    x = lines[i].x >> 8;
  123.    /* Get the x coord */
  124.  
  125.    dest1 = origdest + i * 320;
  126.    dest2 = dest1 + (319 - x);
  127.    source1 = origsource + i * 320;
  128.    source2 = source1 + x;
  129.    
  130.    /* Copy the line in two steps */
  131.    memcpy (dest1, source2, 320 - x);
  132.    if (x > 0)
  133.     memcpy (dest2, source1, x + 1);
  134.    
  135.   }
  136.  
  137. }
  138.  
  139.  
  140.  
  141. /* Animates and displays the running man */
  142. void animate_man (void)
  143. {
  144.  people[0].anm++;
  145.  if (people[0].anm > 29)
  146.    people[0].anm = 0;
  147.  
  148.  wputblock (people[0].x, people[0].y, sprites[people[0].anm], 1);
  149. }
  150.  
  151.  
  152.  
  153. void main (void)
  154. {
  155.  
  156.  vga256 ();
  157.  
  158.  load_graphics ();
  159.  
  160.  init_lines ();
  161.  
  162.  /* Set the position of the running man */
  163.  people[0].x = 120;
  164.  people[0].y = 50;
  165.  people[0].anm = 0;
  166.  
  167.  
  168.  do { 
  169.   wsetscreen (work);
  170.   animate_lines ();
  171.   animate_man ();   
  172.   wnormscreen ();
  173.   wretrace ();
  174.   wputblock (0, 0, work, 0);
  175.   } while (!kbhit ());
  176.  
  177.  free_graphics ();
  178.  wsetmode (3);
  179. }
  180.